import Vue from "vue";

import VueRouter from "vue-router";

Vue.use(VueRouter);

const router = new VueRouter({
    mode: 'hash',
    routes: [
        {
            path: '/home',
            name: 'home',
            redirect: '/welcome',
            component: () => import('@/pages/home/Home.vue'),
            children: [
                {
                    path: '/welcome',
                    component: () => import('@/pages/home/welcome/Welcome.vue')
                },
                {
                    path: '/users',
                    name: 'users',
                    component: () => import('@/pages/home/users/UserList.vue')
                },
                {
                    path: '/roles',
                    name: 'roles',
                    component: () => import('@/pages/home/rights/PermissionList.vue')
                },
                {
                    path: '/rights',
                    name: 'rights',
                    component: () => import('@/pages/home/rights/RoleList.vue')
                },
                {
                    path: '/goods',
                    name: 'goods',
                    component: () => import('@/pages/home/goods/Category.vue')
                },
                {
                    path: '/params',
                    name: 'params',
                    component: () => import('@/pages/home/goods/Classification.vue')
                },
                {
                    path: '/categories',
                    name: 'categories',
                    component: () => import('@/pages/home/goods/ProductList.vue')
                },
                {
                    path: '/orders',
                    name: 'orders',
                    component: () => import('@/pages/home/orders/OrderList.vue')
                },
                {
                    path: '/reports',
                    name: 'reports',
                    component: () => import('@/pages/home/reports/ReportList.vue')
                }
            ]

        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/pages/login/Login.vue')
        },
        {
            path: '/',
            redirect: '/login'
        }
    ]
})
router.beforeEach((to, from, next) => {
    // if(to.path != '/login') {
    //     if(window.sessionStorage.getItem('token')){
    //         next()
    //     }else {
    //         next('/login')
    //     }

    // }
    // next();

    if (to.path == '/login') {
        return next()
    }
    if (!window.sessionStorage.getItem('token')) {
        return next('/login')
    }
    next();
})

export default router